const DataLoader = require('dataloader')
import { Tag } from '../../models'
import { groupData } from '../../lib/graphql'

export const tagLoaderByFolder = () =>
  new DataLoader(
    async keys => {
      const data = await Tag.findAll({
        where: { folderId: { $in: keys } },
        order: [['show', 'DESC'], ['searchCount', 'DESC']]
      })
      return groupData(keys, data, 'folderId')
    },
    { cacheKeyFn: key => 'FolderTag.' + key.toString() }
  )
